
#include "sysctrl.h"
#include "gpio.h"
#include "peripheral.h"
#include "work.h"
#include "led.h"
#include "key.h"
#include "timer.h"
#include "temp.h"
#include "ddl.h"
#include "iap.h"
#include "flash.h"
#include "sc7a20.h"
#include "beep.h"
#include "wdt.h"

void User_Init(void);
void WDT_Init(void);

extern uint8_t off_cnt2;
extern boolean_t ms10_f;

uint8_t sys_run_10mscnt = 0;

int main(void)
{
		App_Clock_Cfg();
		delay_ms(50);
		User_Init();
		App_Timer0Cfg(50);		//50us
		App_LpUartPortCfg();
		System_ADCInit();
		System_PWMInit();
		SC7A20_Init();
		Flash_Init(6, TRUE);
		WDT_Init();
		Wdt_Start();
    while(1)
    {
        if(ms10_f)
		{
			ms10_f=0;
	//		SC7A20_Handler();		//陀螺仪
			IAP_Handler();			//跳转boot程序
			Key_Scan();					//按键检测
			Work_CountTime();		//工作定时
			Beep_Handler();		//蜂鸣器
			LED_Handle();			//LED控制
			Temp_Protect();		//温度保护
			DEBUG_Handler();	//debug
			Work_Driver();			//工作驱动
			
			sys_run_10mscnt ++;
			if(sys_run_10mscnt%20 == 0){
				Wdt_Feed();		// 每200ms，清一次看门狗
			}
		}
		Work_Handler();
    }
}

void WDT_Init()
{
		Sysctrl_SetPeripheralGate(SysctrlPeripheralWdt,TRUE);
    Wdt_Init(WdtResetEn, WdtT820ms);
}

void User_Init()
{
		stc_gpio_cfg_t stcGpioInit = {0};
		
		Sysctrl_SetPeripheralGate(SysctrlPeripheralGpio, TRUE);
		
//		M0P_SYSCTRL->SYSCTRL2 = 0x5A5A;
//    M0P_SYSCTRL->SYSCTRL2 = 0xA5A5;
//		M0P_SYSCTRL->SYSCTRL1|=0x100;
		
		EMS_VCC_PWM_OFF;
		stcGpioInit.enDir = GpioDirOut;
		stcGpioInit.enDrv = GpioDrvH;
		stcGpioInit.enPu = GpioPuDisable;
    stcGpioInit.enPd = GpioPdDisable;
    Gpio_Init(EMS_VCC_PWM_PORT,EMS_VCC_PWM_PIN,&stcGpioInit);
		
		stcGpioInit.enDir = GpioDirOut;
		stcGpioInit.enPu = GpioPuDisable;
    Gpio_Init(GpioPortD,GpioPin5,&stcGpioInit);
		
		stcGpioInit.enDir = GpioDirOut;
		stcGpioInit.enPu = GpioPuDisable;
    Gpio_Init(EMS_SW_EN_PORT,EMS_SW_EN_PIN,&stcGpioInit);
		EMS_SW_EN_OFF;
		
		stcGpioInit.enDir = GpioDirOut;
		stcGpioInit.enPu = GpioPuDisable;
    Gpio_Init(LED7_PORT,LED7_PIN,&stcGpioInit);
		LED7_OFF;
		
		stcGpioInit.enDir = GpioDirOut;
		stcGpioInit.enPu = GpioPuDisable;
    Gpio_Init(LED6_PORT,LED6_PIN,&stcGpioInit);
		LED6_OFF;
		
		stcGpioInit.enDir = GpioDirOut;
		stcGpioInit.enPu = GpioPuDisable;
    Gpio_Init(LED8_PORT,LED8_PIN,&stcGpioInit);
		LED8_OFF;

		stcGpioInit.enDir = GpioDirOut;
		stcGpioInit.enPu = GpioPuDisable;
    Gpio_Init(EMS_VCC_EN_PORT,EMS_VCC_EN_PIN,&stcGpioInit);
		EMS_VCC_EN_OFF;

		stcGpioInit.enDir = GpioDirOut;
		stcGpioInit.enPu = GpioPuDisable;
    Gpio_Init(GPIO_IPL_PORT,GPIO_IPL_PIN,&stcGpioInit);
		GPIO_IPL_OFF;

		Gpio_SetAnalogMode(ISET_PORT, ISET_PIN);
		stcGpioInit.enDir = GpioDirIn;
		stcGpioInit.enPu = GpioPuDisable;
    Gpio_Init(ISET_PORT,ISET_PIN,&stcGpioInit);

		stcGpioInit.enDir = GpioDirOut;
		stcGpioInit.enPu = GpioPuDisable;
    Gpio_Init(BOOST_Control_PORT,BOOST_Control_PIN,&stcGpioInit);
		BOOST_Control_OFF;

		stcGpioInit.enDir = GpioDirOut;
		stcGpioInit.enPu = GpioPuDisable;
    Gpio_Init(GPIO_Control_Ice_PORT,GPIO_Control_Ice_PIN,&stcGpioInit);
		GPIO_Control_Ice_OFF;

		Gpio_SetAnalogMode(ADC_300V_PORT, ADC_300V_PIN);
		stcGpioInit.enDir = GpioDirIn;
		stcGpioInit.enPu = GpioPuDisable;
    Gpio_Init(ADC_300V_PORT,ADC_300V_PIN,&stcGpioInit);
		
		Gpio_SetAnalogMode(ADC_24V_PORT, ADC_24V_PIN);
		stcGpioInit.enDir = GpioDirIn;
		stcGpioInit.enPu = GpioPuDisable;
    Gpio_Init(ADC_24V_PORT,ADC_24V_PIN,&stcGpioInit);
		
		stcGpioInit.enDir = GpioDirOut;
		stcGpioInit.enPu = GpioPuDisable;
    Gpio_Init(FAN_PORT,FAN_PIN,&stcGpioInit);
		FAN_OFF;

		stcGpioInit.enDir = GpioDirIn;
		stcGpioInit.enPu = GpioPuEnable;
    Gpio_Init(FAN_CHECK_PORT,FAN_CHECK_PIN,&stcGpioInit);

		Gpio_SetAnalogMode(NTC_FAN_PORT, NTC_FAN_PIN);
		stcGpioInit.enDir = GpioDirIn;
		stcGpioInit.enPu = GpioPuDisable;
    Gpio_Init(NTC_FAN_PORT,NTC_FAN_PIN,&stcGpioInit);

		stcGpioInit.enDir = GpioDirIn;
		stcGpioInit.enPu = GpioPuEnable;
    Gpio_Init(K2_PORT,K2_PIN,&stcGpioInit);

		stcGpioInit.enDir = GpioDirOut;
		stcGpioInit.enPu = GpioPuDisable;
    Gpio_Init(LED1_PORT,LED1_PIN,&stcGpioInit);
		LED1_OFF;

		Gpio_SetAnalogMode(EMS_VCC_ADC_PORT, EMS_VCC_ADC_PIN);
		stcGpioInit.enDir = GpioDirIn;
		stcGpioInit.enPu = GpioPuDisable;
    Gpio_Init(EMS_VCC_ADC_PORT,EMS_VCC_ADC_PIN,&stcGpioInit);

		stcGpioInit.enDir = GpioDirIn;
		stcGpioInit.enPu = GpioPuEnable;
    Gpio_Init(EMS_ISEN_PORT,EMS_ISEN_PIN,&stcGpioInit);

		stcGpioInit.enDir = GpioDirOut;
		stcGpioInit.enPu = GpioPuDisable;
    Gpio_Init(LED2_PORT,LED2_PIN,&stcGpioInit);
		LED2_OFF;

		stcGpioInit.enDir = GpioDirOut;
		stcGpioInit.enPu = GpioPuDisable;
    Gpio_Init(LED3_PORT,LED3_PIN,&stcGpioInit);
		LED3_OFF;
		
		stcGpioInit.enDir = GpioDirOut;
		stcGpioInit.enPu = GpioPuDisable;
    Gpio_Init(LED5_PORT,LED5_PIN,&stcGpioInit);
		LED5_OFF;

		BUZZER_OFF;
		stcGpioInit.enDir = GpioDirOut;
		stcGpioInit.enPu = GpioPuDisable;
    Gpio_Init(BUZZER_PORT,BUZZER_PIN,&stcGpioInit);

		stcGpioInit.enDir = GpioDirOut;
		stcGpioInit.enPu = GpioPuDisable;
    Gpio_Init(LED4_PORT,LED4_PIN,&stcGpioInit);
		LED4_OFF;

		stcGpioInit.enDir = GpioDirIn;
		stcGpioInit.enPu = GpioPuEnable;
    Gpio_Init(K1_PORT,K1_PIN,&stcGpioInit);

		stcGpioInit.enDir = GpioDirIn;
		stcGpioInit.enPu = GpioPuEnable;
    Gpio_Init(K3_PORT,K3_PIN,&stcGpioInit);

		stcGpioInit.enDir = GpioDirIn;
		stcGpioInit.enPu = GpioPuEnable;	// 20250605 touch脚低电平有效，所以开启上拉电阻
    Gpio_Init(Touch2_PORT,Touch2_PIN,&stcGpioInit);

		stcGpioInit.enDir = GpioDirOut;
		stcGpioInit.enPu = GpioPuDisable;
    Gpio_Init(EMS_PWM_P_PORT,EMS_PWM_P_PIN,&stcGpioInit);
		EMS_PWM_P_OFF;

		stcGpioInit.enDir = GpioDirOut;
		stcGpioInit.enPu = GpioPuDisable;
    Gpio_Init(Touch1_PORT,Touch1_PIN,&stcGpioInit);
		Touch1_ON;

		stcGpioInit.enDir = GpioDirOut;
		stcGpioInit.enPu = GpioPuDisable;
    Gpio_Init(LED_EN_PORT,LED_EN_PIN,&stcGpioInit);
		LED_EN_OFF;

		stcGpioInit.enDir = GpioDirOut;
		stcGpioInit.enPu = GpioPuDisable;
    Gpio_Init(EMS_PWM_N_PORT,EMS_PWM_N_PIN,&stcGpioInit);
		EMS_PWM_N_OFF;

		off_cnt2=60;
}



